package com.inspect;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.inspect.dbConn.dbSingleton;
import com.inspect.log.LogSingleton;
import com.inspect.quartz.JobSchedule;
import com.inspect.util.Constant;

public class DailyJob implements Job {
	private static Logger log = LogSingleton.get_logger(DailyJob.class);

	public void clearTaskChange() {
		String ly_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
		log.info("清理任务变更表:" + ly_time);
		dbSingleton db = dbSingleton.get_instance();
		String sql = "truncate table t_inspect_task_change";
		db.db_execute(sql);
	}

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		JobSchedule jobSchedule = new JobSchedule();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ly_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
		log.info("开始执行日常任务:" + ly_time);

		dbSingleton db = dbSingleton.get_instance();

		/*String sql = "select h.*, g.default_script as private_threshold, k.start_time "
				+ "from (select t.change, t.plan_id||'_'||t.script_id||'_'||t.asset_code as job_id,"
				+ "t.plan_id,t.script_id,t.asset_code,a.host_ip,a.uname,a.zone, a.pwd, a.port,"
				+ "a.instance_name as sid,b.script_name,b.default_script,c.cron_expression "
				+ "from t_inspect_task_change t,t_inspect_asset a,t_inspect_script b,t_inspect_plan c  "
				+ "where t.plan_id = c.plan_id and t.script_id = b.script_id and t.asset_code = a.asset_code) h "
				+ "left join t_inspect_threshold g on g.script_id = h.script_id and "
				+ "g.asset_code = h.asset_code left join t_inspect_cornexpress_tmp k on k.plan_id = h.plan_id";*/
		String sql = "select t.change,t.plan_id,t.plan_status,t.script_id,"
				+ "p.CRON_EXPRESSION,c.start_time,p.ip, " + 
				" t.plan_id||'_'||t.ip||'_'||t.script_id JOB_ID,script_name,p.sys_type " +
				"from t_inspect_task_change t " + 
				"left join t_inspect_plan p on t.plan_id=p.plan_id " + 
				"left join t_inspect_cornexpress_tmp c on c.plan_id=t.plan_id " +
				"left join T_INSPECT_PLANSCRIPT_RP rp on t.plan_id=rp.plan_id and T.Script_Id=rp.script_id";

		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		list = db.db_query_assoc(sql);
		if (list != null) {
			for (HashMap<String, String> hashMap : list) {
				try {
					String change = hashMap.get("CHANGE").trim();
					String id = hashMap.get("JOB_ID");
					String corn_pattern = hashMap.get("CRON_EXPRESSION");
					String start_time = hashMap.get(Constant.START_TIME);
					String className = "com.inspect.protocol.Inspect_sz";
					Object c = Class.forName(className).newInstance();
					if (Constant.ADD.equals(change)) {
						log.info("检测到需要增加一个任务" + id);
						jobSchedule.addJob(id, Constant.defailtJobGroup, id, Constant.defaultTriggerGroup, c.getClass(),
								corn_pattern, sdf.parse(start_time), hashMap);
					} else if (Constant.REMOVE.equals(change)) {
						log.info("检测到需要移除一个任务" + id);
						jobSchedule.removeJob(id, Constant.defailtJobGroup, id, Constant.defaultTriggerGroup);
					} else if (Constant.EDIT.equals(change)) {
						log.info("检测到需要修改一个任务" + id);
						jobSchedule.editJob(id, Constant.defailtJobGroup, id, Constant.defaultTriggerGroup,
								c.getClass(), corn_pattern, sdf.parse(start_time), hashMap);
					} else {
						log.error("意外状况");
					}
				} catch (Exception e) {
					log.error("Daily:" + e);
				}
			}
			String truncateSQL = "truncate table t_inspect_task_change";
			db.db_execute(truncateSQL);
		} else {
			log.info("巡检任务没有改变");
		}
		ly_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
		log.info("结束日常任务:" + ly_time);
	}

}
